package com.example.musicgui.tools;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.example.musicgui.model.ResponseBodyMessage;
import com.example.musicgui.model.User;

import java.util.HashMap;
import java.util.Map;

public class ObjuctOrJSON<T>{
    public static String toJSON(Map<String,String> map) throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(map);
    }

    public ResponseBodyMessage<T> User(String request){
        return getBasic(result(request));
    }

    private ResponseBodyMessage<T> getBasic(ResponseBodyMessage<String> result) {
        Boolean status = result.getStatus();
        String message = result.getMessage();
        System.out.println(result.getData());
        T data = (T)result.getData();
        return new ResponseBodyMessage<>(status,message,data);
    }

    public ResponseBodyMessage<String> result(String request){
        String[] a = request.split(";");
        String[] b  = new String[3];
        for (int i = 0; i < a.length; i++) {
            String[] c = a[i].split("=");
            b[i] = c[1];
        }
        Boolean status = Boolean.valueOf(b[0]);
        return new ResponseBodyMessage<>(status,b[1],b[2]);
    }
}
